package com.flutter.aliyun_push

import android.app.Application
import android.os.Handler
import android.util.Log
import androidx.annotation.NonNull
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import com.alibaba.sdk.android.push.CommonCallback

/** AliyunPushPlugin */
class AliyunPushPlugin : FlutterPlugin, MethodCallHandler {
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        var channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.aliYun_push")
        AliYunPushHandler.methodChannel = channel
        channel.setMethodCallHandler(this)
    }

    companion object {
        private val TAG = "Init"
        private val inHandler = Handler()
        @JvmStatic
        fun initPushService(application: Application) {
            PushServiceFactory.init(application.applicationContext)
            val pushService = PushServiceFactory.getCloudPushService()
            pushService.register(application.applicationContext, object : CommonCallback {
                override fun onSuccess(response: String?) {
                    Log.d(TAG, "init cloudchannel success")
                    val deviceId = PushServiceFactory.getCloudPushService().deviceId //3edce3a68d604974875cf1b5a2893995
                    Log.d(TAG, "deviceId= $deviceId")

                    inHandler.postDelayed({
                        AliYunPushHandler.methodChannel?.invokeMethod("initCloudChannelResult", mapOf(
                                "isSuccessful" to true,
                                "response" to response
                        ))
                    },2000)
                    println("init cloudchannel success")

                }

                override fun onFailed(errorCode: String, errorMessage: String) {
                    Log.d(TAG, "init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage")
                    inHandler.postDelayed({
                        AliYunPushHandler.methodChannel?.invokeMethod("initCloudChannelResult", mapOf(
                                "isSuccessful" to false,
                                "errorCode" to errorCode,
                                "errorMessage" to errorMessage
                        ))
                    },2000)
                    println("init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage")
                }
            })
        }
    }


    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}
